package org.restorator.utill;

import com.google.gson.Gson;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.Marker;
import org.slf4j.MarkerFactory;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Writer;

/**
 * @author Snisar Bogdan.
 */
public class ControllerUtil {

    private static Logger logger = LoggerFactory.getLogger(GeneralUtil.getClassName());
    private static Marker error = MarkerFactory.getMarker("ERROR");

    public static final String OK = "{\"success\":true, \"data\":";
    public static final String ERROR_MESEG = "{\"success\":false, \"error\":";
    public static final String ERROR = "{\"success\":false}";
    public static final String END_STREAM = "}";

    /*
    * Return id as the end of url, or dispatch to real url if the end of line is not digital.
    */
    public static String getUrlIdMatch(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        String url = req.getRequestURI();
        String[] array = url.split("/");
        String urlStringEnd = (array.length > 0) ? array[array.length - 1] : "";
        logger.trace("match url ends to {}", urlStringEnd);

        return urlStringEnd;
    }

    public static void printOk(Writer w, Object massage) throws IOException {

        w.write(OK);
        Gson g = new Gson();
        w.write(g.toJson(massage));
        w.write(END_STREAM);
        w.flush();

    }

    /**
     * Print ok message in special format and setContentType("application/json")
     * @param resp {@link HttpServletResponse} response
     * @param mesg some message
     * @throws IOException
     */
    public static void printOk(HttpServletResponse resp, Object mesg) throws IOException {
        resp.setContentType("application/json");
        Writer w = resp.getWriter();
        w.write(OK);
        Gson g = new Gson();
        String str = g.toJson(mesg);
        w.write(str);
        logger.trace("print OK." + OK + str + END_STREAM);
        w.write(END_STREAM);
        w.flush();
        logger.debug(MarkerFactory.getMarker("SUCCESS"), "Send success response {}", mesg);

    }

    /**
     * Print success message in special format and setContentType("application/json")
     * @param resp {@link HttpServletResponse} response
     * @param mesg some message
     * @throws IOException
     */
    public static void printSuccess(HttpServletResponse resp, Object mesg) throws IOException {
        resp.setContentType("application/json");
        Writer w = resp.getWriter();
        w.write(OK);
        Gson g = new Gson();
        w.write(g.toJson(mesg));
        w.write(END_STREAM);
        w.flush();
        logger.debug(MarkerFactory.getMarker("SUCCESS"), "Send success response {}", mesg);
    }

    public static void printError(HttpServletResponse resp, Object mesg) throws IOException {
        
        resp.setContentType("application/json");
        Writer w = resp.getWriter();
        w.write(ERROR_MESEG);
        Gson g = new Gson();
        String str = g.toJson(mesg);
        w.write(str);
        logger.trace("send error: " + ERROR_MESEG + str + END_STREAM);

        w.write(END_STREAM);
        w.flush();
        logger.debug(MarkerFactory.getMarker("SUCCESS"), "Send error response {}", mesg);
    }

    public static void printSuccess(Writer w) throws IOException {
        w.write("{\"success\":true}");
        w.flush();
    }




    public static void printError(Writer w) throws IOException {
        w.write(ERROR);
        w.flush();
    }

    public static void printError(HttpServletResponse resp) throws IOException {
        resp.setContentType("application/json");
        Writer w = resp.getWriter();
        w.write(ERROR);
        w.flush();
    }

    public static void printError(Writer w, Object o) throws IOException {
        w.write(ERROR_MESEG);
        Gson g = new Gson();
        w.write(g.toJson(o));
        w.write(END_STREAM);
        w.flush();
    }
}
